<?php
/**
 * Administrative functions for the Content Management System
 *
 * ACCESS: ADMINS ONLY
 *
 * @author Chris Lunsford
 * @version 1.0
 */
class Admin extends Controller {
	
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->badger->checkAllow('admin');
	}
	
	/**
	 * Current landing page for admins.  Will end up being a dashboard, for now
	 * just redirects to the blog page.
	 */
	function index() {
		redirect('admin/blog');
	}
	
	/******************** URL Functions ********************/
	
	/**
	 * Displays page: Static content
	 */
	function content() {
		$data = $this->_buildContent();
		
		$this->load->model('ContentModel');
		$data['biyl'] = $this->ContentModel->getBIYLs();
		$this->load->view('admin/content', $data);
	}
	
	/**
	 * Displays page: Users
	 */
	function users() {
		$data = $this->_buildContent();
		$data['users'] = $this->UserModel->getUsers();
		$data['unsalted'] = $this->UserModel->countUnsalted();
		$this->load->view('admin/users', $data);
	}
	
	/**
	 * Displays page: Groups
	 */
	function groups() {
		$data = $this->_buildContent();
		if ($this->uri->segment(3) == "show") {
			$data['users'] = $this->UserModel->getGroupMembers($this->uri->segment(4));
			$data['group'] = $this->UserModel->getGroup($this->uri->segment(4));
			$this->load->view('admin/groups_show',$data);
		} else {
			$data['groups'] = $groups = $this->UserModel->getGroups();
			$users = $this->UserModel->getUsers('firstname'); //sort by first name
			
			foreach ($groups->result() as $group) {
				$data['dropdown_groups'][$group->groupid] = $group->groupname;
			}
	
			foreach ($users->result() as $user) {
					$data['dropdown_users'][$user->userid] = $this->UserModel->getDisplayName($user->userid);
			}
			
			$data['dropdown_rank'] = array('0' => 'Member', '1' => 'Leader');
			foreach ($groups->result() as $row) {
				$row->members = $this->UserModel->getMemberCount($row->groupid);
				$row->leaders = $this->UserModel->getLeaders($row->groupid,'array');
			}
			$this->load->view('admin/groups', $data);
		}
	}
	
	/**
	 * Displays page: Edit a Group Name / Description
	 */
	function editGroup() {
		$data = $this->_buildContent();
		$data['group'] = $this->UserModel->getGroup($this->uri->segment(3));
		$this->load->view('admin/group_edit', $data);
	}
	
	/**
	 * Displays page: Events
	 */
	function events() {
		$data = $this->_buildContent();
		$this->load->model('CalendarModel');
		
		$events = $this->CalendarModel->getAll();
		foreach($events->result() as $row){
			$row->num_items = $this->CalendarModel->getItems($row->id)->num_rows();
		}
		$data['events'] = $events;
		$this->load->view('admin/events', $data);
	}
	
	/**
	 * Displays page: Blog
	 */
	function blog() {
		$data = $this->_buildContent();
		$this->load->model('BlogModel');
		$posts = $this->BlogModel->getPosts();
		foreach($posts->result() as $row) {
			$row->author = $this->UserModel->getDisplayName($row->authorid);
			$row->posted = date('M d, y - h:ia', $row->posted);
			//if ($row->edited != '0') {
			//	$row->edit = date('M d, y - h:ia', $row->edited).' ('. $this->UserModel->getDisplayName($row->edited_by).')';
			//} else {
			//	$row->edit = "";
			//}
			$row->num_comments = $this->BlogModel->getComments($row->id)->num_rows();
			$row->public = ($row->public == '1') ? 'Public' : 'Private';
			$row->published = ($row->published == '1') ? 'Published' : 'Draft';
		}
		
		$data['posts'] = $posts;
		$this->load->view('admin/blog', $data);
	}
	
	/**
	 * Displays page: Blog Comments
	 */
	function blog_comments() {
		$data = $this->_buildContent();
		$this->load->model('BlogModel');
		$comments = $this->BlogModel->getComments($this->uri->segment(3));
		foreach($comments->result() as $row) {
			$row->author = $this->UserModel->getUsername($row->authorid);
			$row->posted = date('F j, Y - g:ia', $row->posted);
		}
		
		$data['comments'] = $comments;
		$this->load->view('admin/blog_comments', $data);
	}
	
	/******************* Task Functions *******************/
	
	/**
	 * Assigns a user to a group as leader or member.
	 */
	function assign_user() {
		$userid = $this->input->post('userid');
		$groupid = $this->input->post('groupid');
		if ($this->UserModel->isUserInGroup($userid,$groupid)) {
			//tell the user that they've already been assigned somehow
			$this->session->set_flashdata('msg_error','This user has already been assigned to this group!');
			redirect('admin/groups');
		} else {
			$post['userid'] = $this->input->post('userid');
			$post['groupid'] = $this->input->post('groupid');
			$post['rank'] = $this->input->post('rank');
			$this->UserModel->assignUser($post);
			$this->session->set_flashdata('msg_info','User added to group successfully!');
			redirect('admin/groups');
		}
	}
	
	/**
	 * Removes a user from a group
	 */
	function unassignUser() {
		$this->UserModel->unassignUser($this->uri->segment(4),$this->uri->segment(3));
		redirect('admin/groups/show/'.$this->uri->segment(4));
	}
	
	/**
	 * Deletes a user account
	 */
	function deleteUser() {
		$userid = $this->uri->segment(3);
		$this->UserModel->deleteUser($userid);
		redirect('admin/users');
	}
	
	/**
	 * Creates a new group
	 */
	function createGroup() {
		//needs validation!!
		$post['groupname'] = $this->input->post('groupname',TRUE);
		$post['description'] = $this->input->post('description',TRUE);
		
		$this->UserModel->createGroup($post);
		redirect('admin/groups');
	}
	
	/**
	 * Deletes the specified group
	 *
	 * @param groupid the id of the group being deleted
	 */
	function deleteGroup() {
		$groupid = $this->uri->segment(3);
		$this->UserModel->deleteGroup($groupid);
		redirect('admin/groups');
	}
	
	/**
	 * Edits group and updates with submitted information
	 */
	function doEditGroup() {
		//needs validation!!
		$post['groupname'] = $this->input->post('groupname', TRUE);
		$post['description'] = $this->input->post('description', TRUE);
		$groupid = $this->input->post('groupid');
			
		$this->UserModel->updateGroup($groupid,$post);
		redirect('admin/groups');
	}
	
	/**
	 * Changes the rank of a user in a group, either to leader or member.
	 *
	 * @param userid the user being updated
	 * @param groupid id of the group the user is in
	 * @param rank the new rank of the user (1 = Leader, 0 = Member)	
	 */
	function changeRank() {
		$userid = $this->uri->segment(3);
		$groupid = $this->uri->segment(4);
		$rank = $this->uri->segment(5);
		
		$this->UserModel->updateRank($userid,$groupid,$rank);
		redirect('admin/groups/show/'.$groupid);
	}	
	
	/**
	 * Supplies user-specific information to the interface for customization
	 */
	function _buildContent() {
		//REQUIRED//
		$data['pagetype'] = 'page_admin';
		$data['admin_nav'] = TRUE;
		if ($this->session->flashdata('msg_info')) $data['msg_info'] = $this->session->flashdata('msg_info');
		if ($this->session->flashdata('msg_error')) $data['msg_error'] = $this->session->flashdata('msg_error');
		
		//test if user is logged in for site navigation
		if ($this->session->userdata('loggedin')) {
			$data['loggedin'] = TRUE;
		}
		
		return $data;
	}
}
?>